home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 220 / 220.xpi / chrome / flashgot.jar / content / flashgot / flashgotOptions.js < prev    next >
Encoding:
Text File  |  2010-01-24  |  16.0 KB  |  536 lines

  1. /***** BEGIN LICENSE BLOCK *****
  2.  
  3.     FlashGot - a Firefox extension for external download managers integration
  4.     Copyright (C) 2004-2009 Giorgio Maone - g.maone@informaction.com
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.                              
  20. ***** END LICENSE BLOCK *****/
  21.  
  22. const g_rxOpt=/^(inv|)opt-(.*)/;
  23. var g_downloadManagers = null;
  24. var g_opts = null;
  25. var g_extList = null;
  26. var g_extText = null;
  27. var g_referrerRadio = null;
  28. var g_referrerText = null;
  29. var g_tmpDir = null;
  30. var g_wellGetPath = null;
  31.  
  32. var g_cust = gFlashGotService && {
  33.   dummy: gFlashGotService.createCustomDM(""),
  34.   map: {},
  35.   createEntry: function(obj, isNew) {
  36.     if(!obj.custom) return null;
  37.     const name=obj.name;
  38.     const map=this.map;
  39.     return map[name] ? map[name]
  40.       : map[name]={
  41.           _dirty: false,
  42.           _deleted: false, 
  43.           _new: isNew,
  44.           supported: true,
  45.           name: name,
  46.           argsTemplate: obj.argsTemplate,
  47.           exeFile: isNew?obj.locateExeFile(name):obj.exeFile
  48.         };
  49.   },
  50.   get current() {
  51.     if(!g_downloadManagers.selectedItem) return null;
  52.     const dmName=g_downloadManagers.selectedItem.getAttribute("label");
  53.     
  54.     if( (!dmName) || dmName=="---") return null;
  55.     var dm = gFlashGotService.DMS[dmName];
  56.     if(dm) {
  57.       return this.createEntry(dm,false);
  58.     }
  59.     this.dummy.name=dmName;
  60.     return this.createEntry(this.dummy,true);
  61.   },
  62.   add: function() {
  63.     const ps = Components.classes["@mozilla.org/embedcomp/prompt-service;1"
  64.       ].getService(Components.interfaces.nsIPromptService);
  65.     const ret={ value: "" };
  66.     const title=gFlashGotService.getString("custom.new.title");
  67.     var name;
  68.     if(ps.prompt(window,
  69.           title,
  70.           gFlashGotService.getString("custom.new.text"),
  71.           ret, null, {}) && 
  72.        (name = ret.value) ) {
  73.       if(/,|^(?:\d+|\s+|---)$/.test(name) ||
  74.           gFlashGotService.DMS[name] || this.map[name]) {
  75.         ps.alert(window,title,gFlashGotService.getString("custom.new.error"));
  76.         return;
  77.       }
  78.       const dummy = this.dummy;
  79.       dummy.name = name;
  80.       this.createEntry(dummy, true);
  81.       fgo_populateDMS(name);
  82.     }
  83.   },
  84.   remove: function() {
  85.     var dm=this.current;
  86.     if(dm) {
  87.       dm._deleted=true;
  88.       fgo_populateDMS();
  89.     }
  90.   },
  91.   locateExe: function() {
  92.     const cur=this.current;
  93.     if(cur) {
  94.       var f=this.dummy.locateExeFile(this.current.name);
  95.       if(f) {
  96.         cur.exeFile=f;
  97.         cur._dirty=true;
  98.       }
  99.       this.syncUI();
  100.     }
  101.   }
  102. ,
  103.   argsChanged: function(txtArgs) {
  104.     const cur=this.current;
  105.     var val=txtArgs.value.replace(/['"`]/g,'');
  106.     if(val!=txtArgs.value) {
  107.       document.getElementById("quoteWarn").showPopup(txtArgs,-1, -1, "tooltip", "topleft", "bottomleft");
  108.       selEnd=txtArgs.selectionEnd;
  109.       txtArgs.value=val;
  110.       try {
  111.         txtArgs.selectionEnd=selEnd-1;
  112.       } catch(ex) {}
  113.     }
  114.     if(cur && cur.argsTemplate!=val) {
  115.       cur.argsTemplate=val;
  116.       cur._dirty=true;
  117.     }
  118.   }
  119. ,
  120.   syncUI: function() {
  121.     var dm = this.current;
  122.     document.getElementById("dmsdeck").setAttribute(
  123.       "selectedIndex", dm ? "0" : "1");
  124.     if(dm) {
  125.       document.getElementById("customDM-exeFile").value = 
  126.         dm.exeFile ? dm.exeFile.path : "";
  127.       document.getElementById("customDM-args").value = dm.argsTemplate;
  128.       var ph=document.getElementById("ph-");
  129.       var popup=ph.parentNode;
  130.       while(ph.nextSibling) {
  131.         popup.removeChild(ph.nextSibling);
  132.       }
  133.       const PHS=this.dummy.PLACEHOLDERS;
  134.       if(PHS) {
  135.         var phName;
  136.         for(var j=0, len=PHS.length; j<len; j++) {
  137.           phName=PHS[j];
  138.           ph=ph.cloneNode(true);
  139.           ph.removeAttribute("hidden");
  140.           ph.setAttribute("id","ph-"+phName);
  141.           ph.setAttribute("label",gFlashGotService.getString("ph."+phName));
  142.           popup.appendChild(ph);
  143.         }
  144.       }
  145.     }
  146.   }
  147. ,
  148.  insertPH: function(id) {
  149.    const phName="["+id.substring(3)+"]";
  150.    const txtArgs=document.getElementById("customDM-args")
  151.    var selStart=txtArgs.selectionStart;
  152.    var selEnd=txtArgs.selectionEnd;
  153.    txtArgs.value=txtArgs.value.substring(0,selStart)+phName+txtArgs.value.substring(selEnd);
  154.    txtArgs.selectionStart=txtArgs.selectionEnd=selStart+phName.length;
  155.  }
  156. ,
  157.  save: function() {
  158.    const map=this.map;
  159.    var dm,target;
  160.    for(var name in map) {
  161.      dm=map[name];
  162.      target=null;
  163.      if(dm._deleted) {
  164.        gFlashGotService.removeCustomDM(name);
  165.      } else if(dm._new) {
  166.        target = gFlashGotService.createCustomDM(name);
  167.      } else if(dm._dirty) {
  168.        target = gFlashGotService.DMS[name];
  169.      }
  170.      if(target) {
  171.        target.argsTemplate=dm.argsTemplate;
  172.        target.exeFile=dm.exeFile;
  173.      }
  174.    }
  175.  }
  176. };
  177. function fgo_onload() {
  178.   if(!gFlashGotService) {
  179.     document.getElementById("mainTabbox").setAttribute("hidden","true");
  180.     document.getElementById("badInstall").removeAttribute("hidden");
  181.     document.getAnonymousElementByAttribute(document
  182.             .getElementById("flashgotOptions"),"dlgtype","cancel")
  183.             .setAttribute("hidden","true");
  184.     return;
  185.   }
  186.   
  187.   if (!gFlashGotService.smUninstaller) {
  188.     document.documentElement.getButton("extra2").hidden = true;
  189.   }
  190.   
  191.   try {
  192.       g_wellGetPath = gFlashGotService.prefs.getComplexValue("WellGet.path", Components.interfaces.nsILocalFile);
  193.       document.getElementById("wellget-text").value = g_wellGetPath.path;
  194.   } catch(ex) {}
  195.    
  196.   
  197.   if(gFlashGotService.mailer) { 
  198.     // Thunderbird will handle "simple" clicks through Firefox
  199.     document.getElementById("tab-downloads").setAttribute("collapsed","true");
  200.   }
  201.   
  202.   g_downloadManagers = document.getElementById("downloadManagers");
  203.   g_opts = document.getElementsByTagName("checkbox");
  204.   
  205.   fgo_populateDMS();
  206.   
  207.   var j,len;
  208.   
  209.   g_extList = document.getElementById("ext-list");
  210.   g_extText = document.getElementById("ext-text");
  211.   g_extList.removeItemAt(0);
  212.  
  213.   for each(var e in gFlashGotService.extensions) {
  214.     if (e) g_extList.appendItem(e, e);
  215.   }
  216.   
  217.   fgo_visitCheckboxes(
  218.     function(prefName,inverse,checkbox) {
  219.       var val=gFlashGotService.getPref(prefName);
  220.       checkbox.checked=inverse?!val:val;
  221.     }
  222.   );
  223.   
  224.   g_referrerRadio=document.getElementById("referrer-radio");
  225.   g_referrerText=document.getElementById("referrer-text");
  226.   g_referrerRadio.selectedIndex=gFlashGotService.getPref("autoReferrer") ? 0 : 1;
  227.   g_referrerText.value=gFlashGotService.getPref("fakeReferrer","");
  228.   fgo_syncReferrer();
  229.   g_referrerRadio.addEventListener("select", fgo_syncReferrer, true);
  230.   
  231.   
  232.   g_downloadManagers.addEventListener("popuphidden", fgo_syncDMOptions, true);
  233.   try {
  234.     g_tmpDir = gFlashGotService.prefs.getComplexValue("tmpDir", Components.interfaces.nsILocalFile);
  235.   } catch(ex) {
  236.      g_tmpDir = gFlashGotService.tmpDir.parent;
  237.   }
  238.   if(g_tmpDir) {
  239.     document.getElementById("tmpdir-text").value =g_tmpDir.path;
  240.   }
  241.   
  242.   document.getElementById("interception-rg").selectedItem=
  243.     document.getElementById("intercept"+
  244.       (gFlashGotService.getPref("interceptAll")?"All":"Ext")+"-radio");
  245. }
  246.  
  247. function fgo_populateDMS(name) {
  248.   const defaultDM = gFlashGotService.defaultDM;
  249.   const dms = gFlashGotService.DMS;
  250.   const dmList = [].concat(gFlashGotService.DMS);
  251.  
  252.   const map = g_cust.map;
  253.   var dm;
  254.   for(p in map) {
  255.     dm=map[p];
  256.     if( ! (dm._deleted || p in dms) ) {
  257.       dmList.push(map[p]);
  258.     }
  259.   }
  260.   
  261.   if(!name) name = defaultDM;
  262.   
  263.   var j, len, menuItem;
  264.   
  265.   g_downloadManagers.removeAllItems();
  266.   g_downloadManagers.selectedItem=null;
  267.   
  268.   var custom;
  269.   var found=false;
  270.   for(j = 0, len=dmList.length; j<len; j++) {
  271.     dm=dmList[j];
  272.     if((custom=g_cust.createEntry(dm,false)) && custom._deleted) {
  273.       continue;
  274.     }
  275.     
  276.     menuItem = g_downloadManagers.appendItem(dm.name,dm.codeName);
  277.     
  278.     if(dm.supported) {
  279.       if(!found) {
  280.         found = (name == dm.name);
  281.         if(found || !g_downloadManagers.selectedItem) {
  282.           g_downloadManagers.selectedItem=menuItem;
  283.         }
  284.       }
  285.     } else {
  286.       if(!dm.shouldList()) menuItem.setAttribute("disabled", "true");
  287.     }
  288.   }
  289.   
  290.   
  291.   if(g_downloadManagers.selectedItem) {
  292.     document.getElementById("nodms").setAttribute("collapsed","true");
  293.   } else {
  294.     g_downloadManagers.selectedItem=g_downloadManagers.appendItem("---",null);
  295.     document.getElementById("nodms").removeAttribute("collapsed");
  296.   }
  297.   
  298.   fgo_syncDMOptions();
  299. }
  300.  
  301. function fgo_interceptionSelected(rg) {
  302.   document.getElementById('extensions-box').style.visibility=
  303.     rg.selectedItem && rg.selectedItem.id=='interceptAll-radio' ? "hidden":"visible";
  304. }
  305.  
  306. function fgo_syncDMOptions() {
  307.   const dmrx = g_downloadManagers.value ?
  308.     new RegExp("\\b" + g_downloadManagers.value + "\\b")
  309.     :null;
  310.   const dmopts = document.getElementsByAttribute("class", "dm-opt");
  311.   var dmid, dmopt;
  312.   for(var j = dmopts.length; j-->0;) {
  313.     dmopt = dmopts[j];
  314.     if(dmrx) {
  315.       dmid = dmopt.id;
  316.       if(dmid) {
  317.         dmopt.setAttribute("hidden", dmid.match(dmrx) ? "false" : "true" );
  318.       }
  319.     } else {
  320.       dmopt.setAttribute("hidden", "true" );
  321.     }
  322.   }
  323.   var dmName = fgo_currentDmName();
  324.   document.getElementById("shownInContextMenu").checked = dmName && ((dmName in shownInContextMenu) 
  325.     ? shownInContextMenu[dmName] : gFlashGotService.DMS[dmName].shownInContextMenu);
  326.  
  327.   g_cust.syncUI();
  328. }
  329.  
  330. function fgo_syncReferrer() {
  331.   if(g_referrerRadio.selectedIndex == 1) {
  332.     g_referrerText.removeAttribute("disabled");
  333.   } else {
  334.     g_referrerText.setAttribute("disabled","true");
  335.   }
  336. }
  337. function fgo_enable(id,enabled) {
  338.   var b=document.getElementById(id);
  339.   if(enabled) {
  340.     b.removeAttribute("disabled");
  341.   } else {
  342.      b.setAttribute("disabled","true");
  343.   }
  344. }
  345.  
  346. function fgo_extText_changed() {
  347.   var enable;
  348.   var value = g_extText.value;
  349.   try {
  350.     if((!g_extText.disabled) &&
  351.       /^[\w\-]+$/.test(value)) {
  352.         enable = true;
  353.         for(var j = g_extList.getRowCount();
  354.             j-- >0 && (enable = g_extList.getItemAtIndex(j).value != value)
  355.             ;);
  356.     } else {
  357.       enable = false;
  358.     }
  359.   } catch(e) {
  360.     dump(e + "\n" + j + "\n");
  361.   }
  362.   fgo_enable("ext-add-button", enable);
  363. }
  364.  
  365. function fgo_extList_changed() {
  366.   fgo_enable("ext-remove-button", g_extList.selectedCount > 0);
  367.   fgo_extText_changed();
  368. }
  369.  
  370. function fgo_ext_add() {
  371.   if (g_extList.getRowCount()) {
  372.     g_extList.insertItemAt(0, g_extText.value, g_extText.value);
  373.   } else {
  374.     g_extList.appendItem(g_extText.value, g_extText.value);
  375.   }
  376.   fgo_extText_changed();
  377. }
  378.  
  379. function fgo_ext_remove() {
  380.   const selectedItems=g_extList.selectedItems;
  381.   for(var j = selectedItems.length; j-->0;) {
  382.     g_extList.removeItemAt(g_extList.getIndexOfItem(selectedItems[j]));
  383.   }
  384. }
  385.  
  386. function fgo_currentDmName() {
  387.   var dmName;
  388.   return g_downloadManagers.selectedItem && (dmName = g_downloadManagers.selectedItem.getAttribute("label"))
  389.     && gFlashGotService.DMS[dmName] && gFlashGotService.DMS[dmName].supported && dmName || null;
  390.  }
  391.  
  392. function fgo_save() {
  393.   if(!gFlashGotService) return true;
  394.   
  395.   fgo_visitCheckboxes(
  396.     function(prefName,inverse,checkbox) {
  397.       gFlashGotService.setPref(prefName,inverse ?! checkbox.checked : checkbox.checked);
  398.     }
  399.   );
  400.   
  401.   g_cust.save();
  402.   
  403.   
  404.   var dmName;
  405.   for(dmName in shownInContextMenu) {
  406.     gFlashGotService.DMS[dmName].shownInContextMenu = shownInContextMenu[dmName];
  407.   }
  408.   
  409.   dmName = fgo_currentDmName();
  410.   if(dmName) {
  411.     gFlashGotService.defaultDM = dmName;
  412.   }
  413.   
  414.   const extensions = [];
  415.   for(var j = g_extList.getRowCount(); j-- > 0;) {
  416.     try {
  417.         extensions.push(g_extList.getItemAtIndex(j).value);
  418.     } catch(e) {
  419.         dump(e + "\n" + j + "\n");  
  420.     }
  421.   }
  422.   gFlashGotService.extensions = extensions;
  423.   gFlashGotService.setPref("autoReferrer",g_referrerRadio.value=="true");
  424.   gFlashGotService.setPref("fakeReferrer",g_referrerText.value);
  425.   if(g_tmpDir) gFlashGotService.prefs.setComplexValue("tmpDir", Components.interfaces.nsILocalFile, g_tmpDir);
  426.   gFlashGotService.setPref("interceptAll",document.getElementById("interceptAll-radio").selected);
  427.   try {
  428.     if(g_wellGetPath) {
  429.       gFlashGotService.prefs.setComplexValue("WellGet.path", Components.interfaces.nsILocalFile, g_wellGetPath); 
  430.     } else {
  431.       if(gFlashGotService.prefs.prefHasUserValue("WellGet.path")) gFlashGotService.prefs.clearUserPref("WellGet.path");
  432.     }
  433.   } catch(ex) {
  434.     dump(ex);
  435.   }
  436.   gFlashGotService.savePrefs();
  437.   return true;
  438.   
  439. }
  440.  
  441. function fgo_visitCheckboxes(callback) {
  442.   var j,checkbox,match;
  443.   for(j=g_opts.length; j-->0;) {
  444.     checkbox=g_opts[j];
  445.     if((match = checkbox.id.match(g_rxOpt))) {
  446.       callback(match[2],match[1]=="inv",checkbox);
  447.     }
  448.   }
  449. }
  450.  
  451.  
  452. function fgo_showLog() {
  453.   try {
  454.     flashgotUtil.browse(Components.classes['@mozilla.org/network/io-service;1'
  455.       ].getService(Components.interfaces.nsIIOService
  456.       ).newFileURI(gFlashGotService.logFile).spec, "_blank",
  457.       "scrollbars=yes,menubar=yes,titlebar=yes,resizable=yes");
  458.   } catch(ex) { dump(ex.message); }
  459. }
  460.  
  461.  
  462. function fgo_clearLog() {
  463.   gFlashGotService.clearLog();
  464. }
  465.  
  466. function fgo_browseTmpDir() {
  467.   const cc=Components.classes;
  468.   const ci=Components.interfaces;
  469.   const fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  470.   const title="FlashGot - "+document.getElementById("tmpdir-label").value;
  471.   fp.init(window, title, ci.nsIFilePicker.modeGetFolder);
  472.   try {
  473.     fp.displayDirectory = g_tmpDir == null ? gFlashGotService.tmpDir.parent : g_tmpDir;
  474.   } catch (ex) { gFlashGotService.log(ex); }
  475.   fp.appendFilters(ci.nsIFilePicker.filterAll);
  476.   if (fp.show()==ci.nsIFilePicker.returnOK) {
  477.     g_tmpDir = fp.file.QueryInterface(ci.nsILocalFile);
  478.     document.getElementById("tmpdir-text").value = g_tmpDir.path;
  479.     document.getElementById("tmpdir-warning").style.visibility="visible";
  480.   }
  481. }
  482.  
  483. function fgo_browseWellGet(reset) {
  484.   if(reset) {
  485.     g_wellGetPath = null;
  486.     document.getElementById("wellget-text").value = "";
  487.     return;
  488.   }
  489.   const cc=Components.classes;
  490.   const ci=Components.interfaces;
  491.   const fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  492.   const title="FlashGot - "+document.getElementById("wellget-label").value;
  493.   fp.init(window, title, ci.nsIFilePicker.modeOpen);
  494.   fp.appendFilters(ci.nsIFilePicker.filterApps);
  495.   if(!g_wellGetPath) {
  496.     g_wellGetPath = gFlashGotService.profDir.clone();
  497.     g_wellGetPath.QueryInterface(Components.interfaces.nsILocalFile)
  498.                 .initWithPath(g_wellGetPath.path.substring(0,3));
  499.     g_wellGetPath.append("WellGet.exe");
  500.   }
  501.   try {
  502.     fp.displayDirectory = g_wellGetPath.parent;
  503.     fp.defaultString = g_wellGetPath.leafName;
  504.   } catch (ex) { gFlashGotService.log(ex); }
  505.  
  506.   if (fp.show() == ci.nsIFilePicker.returnOK) {
  507.     var f = fp.file.QueryInterface(ci.nsILocalFile);
  508.     if(!(f.exists() && f.isExecutable() &&
  509.         f.path.substring(0,1).toUpperCase() == 
  510.         gFlashGotService.profDir.path.substring(0,1).toUpperCase())) {
  511.       Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  512.                 .getService(Components.interfaces.nsIPromptService)
  513.                 .alert(window, "FlashGot / WellGet", 
  514.                   gFlashGotService.getString("wellget.mustBeSameDrive", 
  515.                             [gFlashGotService.profDir.path.substring(0,1)]));
  516.       return;
  517.     }
  518.     g_wellGetPath = f;
  519.     document.getElementById("wellget-text").value = f.path;
  520.     gFlashGotService.DMS["WellGet"]._supported = null;
  521.   }
  522. }
  523.  
  524. var shownInContextMenu = {};
  525. function fgo_shownInContextMenuClicked(cbx) {
  526.   var dmName = fgo_currentDmName();
  527.   if(dmName) shownInContextMenu[dmName] = cbx.checked;
  528. }
  529.  
  530. function fgo_detectNow() {
  531.   gFlashGotService.DMS = gFlashGotService.checkDownloadManagers(true, true);
  532.   fgo_populateDMS();
  533. }
  534.  
  535.  
  536.